angular.module('Controller')

.controller('QgyMemberManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            city:undefined,
            startTime:undefined,
            endTime:undefined,
            enabled:undefined
            filter:undefined
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.sexList=commonConstant.qgySexList(true)
    ##暂不处理
    $scope.cityList=[{value:undefined,name:'全部'},{value:'301000',name:'上海'},{value:'303000',name:'杭州'}]
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            city:undefined,
            filter:$scope.search.filter
            enabled:$scope.search.enabled
        }
        reqData.startTime=moment($scope.search.startTime).format('YYYY-MM-DD') if $scope.search.startTime
        reqData.endTime=moment($scope.search.endTime).format('YYYY-MM-DD') if $scope.search.endTime
        $http.get('/customerUser',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,enabled:1}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        viewData:(item)->
            $rootScope.pageType='member-detail'
            $rootScope.pageParams={memberId:item.id}
            menuManage.goState('member-detail',true)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/contract/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('合同信息删除成功');
                    $scope.queryData();
                else
                    wfAlert.toastError(resp.message)
            )
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyAppointManageEditCtrl',
            templateUrl: 'templates/business-manage/appointment-manage/appointment-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)